home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Transcript 1.0b1 / DbgPrintF.c next >
Text File  |  1996-06-15  |  3KB  |  109 lines

  1. /***
  2.   *
  3.   *    DbgPrintF.c - Formatted printing to the Transcript application.
  4.   *
  5.   *    Copyright © 1996 Christopher E. Hyde. All rights reserved.
  6.   *
  7.   *    Version:    1.0b1
  8.   *
  9.   ***/
  10.  
  11. #include <PPCToolbox.h>
  12. #include <Processes.h>
  13. #include <AppleEvents.h>
  14. #include <AERegistry.h>
  15.  
  16. #include <stdio.h>
  17. #include <stdarg.h>
  18.  
  19. #include "DbgPrintF.h"
  20.  
  21.  
  22. #define    FailOSErr(s)    if ((s) != noErr) return
  23.  
  24.  
  25. static void    FindAProcess    (OSType typeToFind, OSType creatorToFind,
  26.                                  ProcessSerialNumberPtr processSN, ProcessInfoRecPtr infoRecToFill);
  27. static void    InitDebugPrint    (void);
  28.  
  29.  
  30. // This runs through the process list looking for the indicated application
  31. static void
  32. FindAProcess (OSType typeToFind, OSType creatorToFind,
  33.               ProcessSerialNumberPtr processSN, ProcessInfoRecPtr infoRecToFill)
  34. {
  35.     processSN->lowLongOfPSN  = kNoProcess;
  36.     processSN->highLongOfPSN = kNoProcess;
  37.  
  38.     do {
  39.         FailOSErr(GetNextProcess(processSN));
  40.         FailOSErr(GetProcessInformation(processSN, infoRecToFill));
  41.     } while (infoRecToFill->processSignature != creatorToFind ||
  42.                 infoRecToFill->processType != typeToFind);
  43. }
  44.  
  45.  
  46. static AppleEvent pDbgEvent = { typeNull, nil };
  47.  
  48. enum {
  49. //    kAEMiscStandards    = 'misc',
  50.     kAEEcho                = 'Echo',
  51.     kTranscriptType        = 'APPL',
  52.     kTranscriptCreator    = 'Trns'
  53. };
  54.  
  55.  
  56. static void
  57. InitDebugPrint (void)
  58. {
  59.     Str31            processName;
  60.     FSSpec            procSpec;
  61.     ProcessInfoRec    infoRec;
  62.     ProcessSerialNumber    process;
  63.     AEDesc targetDesc = { typeNull, nil };
  64.  
  65.     infoRec.processInfoLength = sizeof(ProcessInfoRec);
  66.     infoRec.processName       = processName;
  67.     infoRec.processAppSpec    = &procSpec;
  68.  
  69.         // Find the 'Transcript' application on the machine we wish to communicate with
  70.     FindAProcess(kTranscriptType, kTranscriptCreator, &process, &infoRec);
  71.  
  72.     FailOSErr(AECreateDesc(typeProcessSerialNumber, &process, sizeof(process), &targetDesc));
  73.  
  74.         // Create the EchoEvent
  75.     FailOSErr(AECreateAppleEvent(kAEMiscStandards, kAEEcho,
  76.                                  &targetDesc, kAutoGenerateReturnID,
  77.                                  kAnyTransactionID, &pDbgEvent));
  78.  
  79.     AEDisposeDesc(&targetDesc);
  80. }
  81.  
  82.  
  83. void
  84. DbgPrintF (const char* format, ...)
  85. {
  86.     static Ptr pMsgBuf = nil;
  87.     va_list        ap;
  88.     Size        msgLength;
  89.     AppleEvent    aeReply;
  90.  
  91.     if (pMsgBuf == nil) {
  92.         InitDebugPrint();
  93.         pMsgBuf = NewPtr(4096);
  94.         if (pMsgBuf == nil)
  95.             return;
  96.     }
  97.  
  98.     va_start(ap, format);
  99.     msgLength = vsprintf(pMsgBuf, format, ap);
  100.     va_end(ap)
  101.  
  102.     FailOSErr(AEPutParamPtr(&pDbgEvent, keyDirectObject, typeChar, pMsgBuf, msgLength));
  103.  
  104.     FailOSErr(AESend(&pDbgEvent, &aeReply, kAENoReply + kAENeverInteract + kAECanSwitchLayer,
  105.                        kAENormalPriority, kAEDefaultTimeout, nil, nil));
  106.     FailOSErr(AEDeleteParam(&pDbgEvent, keyDirectObject));
  107. }
  108.  
  109.